package com.lyl.xxx.modules.web.account.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.lyl.xxx.common.annotation.Log;
import com.lyl.xxx.common.enums.BusinessType;
import com.lyl.xxx.modules.system.account.service.SysUserService;
import com.lyl.xxx.modules.web.account.model.dto.ModifyPwdDTO;
import com.lyl.xxx.modules.web.account.model.dto.UpdateUserDTO;
import com.lyl.xxx.modules.web.account.model.dto.UserDTO;
import com.lyl.xxx.modules.web.account.model.vo.UserVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Mr.Luo
 * @date 2022-09-22 11:48
 * @desc 用户管理
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController {
    private final SysUserService sysUserService;

    @GetMapping("/list/{pageNo}/{pageSize}")
    public PageInfo<UserVO> list(@PathVariable Integer pageNo, @PathVariable Integer pageSize, UserDTO dto) {
        log.info("方法名: list,  请求参数: pageNo = [" + pageNo + "], pageSize = [" + pageSize + "], dto = [" + dto + "]");
        return sysUserService.list(pageNo, pageSize, dto);
    }

    @DeleteMapping("/delBatch")
    @Log(title = "用户模块",businessType = BusinessType.DELETE)
    public boolean delById(@RequestBody List<Long> ids) {
        log.info("方法名: delById,  请求参数: ids = [" + ids + "]");
        return sysUserService.removeByIds(ids);
    }

    @GetMapping("/{userId}")
    public UserVO getById(@PathVariable Long userId) {
        return BeanUtil.copyProperties(sysUserService.getById(userId), UserVO.class);
    }

    @PostMapping("/update")
    @Log(title = "用户模块",businessType = BusinessType.UPDATE)
    public boolean updateUser(@RequestBody @Valid UpdateUserDTO dto){
        return sysUserService.updateUser(dto);
    }

    @PostMapping("/modifyPwd")
    public boolean modifyPwd(@RequestBody @Valid ModifyPwdDTO dto){
        return sysUserService.modifyPwd(dto);
    }
}
